package sales.seesion.stateless;

import company.entity.Company;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.faces.model.SelectItem;
import javax.persistence.Query;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import sales.entity.LineItem;
import sales.entity.Product;
import sales.entity.SalesQuotation;
import sales.util.exception.QuotationNotFoundException;
import sales.util.exception.SalesException;

@Stateless
@LocalBean
public class SalesQuotationSessionBean {

    @PersistenceContext
    private EntityManager em;

    public SalesQuotation addNewSalesQuotation(Company selectedCompany, List<LineItem> lineItemList, Date validUntil, double totalPrice, double userDiscount) throws SalesException,ParseException {
        SalesQuotation salesQuotation = new SalesQuotation();
        //System.out.print(selectedCompany);
        salesQuotation.setCompany(selectedCompany);
        salesQuotation.setQuotationLineItems(lineItemList);
        salesQuotation.setTotalPrice(totalPrice);
        salesQuotation.setUserDiscount(userDiscount);
      salesQuotation.setValidUntil(validUntil);
      salesQuotation.setRaiseDate(getTodayDate());
               

        em.persist(salesQuotation);
        return salesQuotation;
    }
    
    private Date getTodayDate()throws ParseException{
       Calendar calendar = Calendar.getInstance();
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
 
        
            String dateString= dateFormat.format(calendar.getTime());
            return dateFormat.parse(dateString); 
         
      
    }

    public List<SalesQuotation> getAllSalesQuotations() {
        Query query = em.createQuery("SELECT q1 FROM SalesQuotation q1");
        return query.getResultList();
    }

    public void deleteSalesQuotationLineItems(Long quotationId) {
        SalesQuotation salesQuotation = em.find(SalesQuotation.class, quotationId);
        List<LineItem> lineItems = salesQuotation.getQuotationLineItems();
        for (Object o : lineItems) {
            LineItem li = (LineItem) o;
            em.remove(li);
        }
        salesQuotation.setQuotationLineItems(null);
        em.flush();
    }
    
      public void deleteSalesQuotation(Long quotationId){
        SalesQuotation salesQuotation = em.find(SalesQuotation.class,quotationId);
        List <LineItem> lineItems = salesQuotation.getQuotationLineItems();
        for (Object o: lineItems){
            LineItem li = (LineItem)o;
            em.remove(li);
        }
        em.remove(salesQuotation);
    }

    public void deleteSalesOrder(Long id) {
        SalesQuotation salesQuotationEntity = em.find(SalesQuotation.class, id);
        em.remove(salesQuotationEntity);
    }

    public List<SalesQuotation> getAllSalesQuotation() {
        Query query = em.createQuery("SELECT sl FROM SalesQuotation sl");
        return query.getResultList();
    }

    public Product getProduct(String productName) {
        Query query = em.createQuery("SELECT p FROM Product p WHERE p.productName = :inProductName");
        query.setParameter("inProductName", productName);
        Product product = null;
        try {
            product = (Product) query.getSingleResult();
        } catch (NoResultException ex) {
            ex.printStackTrace();
        }
        return product;
    }
    
    public void saveChanges(SalesQuotation salesQuotation){
        em.merge(salesQuotation);
    }
//    public Product getProductByCode(String productCode) {
//        
//        Query query = em.createQuery("SELECT p FROM Product p WHERE p.productName = :inProductCode"); //should change Product properbility from product Nmae to product Code?
//        //in database, store each product by its code ->A,B,C,D
//        query.setParameter("inProductCode", productCode);
//        Product product = null;
//        try {
//            product = (Product) query.getSingleResult();
//        } catch (NoResultException ex) {
//            ex.printStackTrace();
//        }
//        return product;
//    }
    
    public List<SelectItem> getProductList(SalesQuotation salesQuotation) {
    if(salesQuotation == null)
    {
         return new ArrayList<SelectItem>();
    }
    else
    {
    ArrayList result = new ArrayList();
    List<LineItem> lineItems = salesQuotation.getQuotationLineItems();
    
    List<SelectItem> selectItems = new ArrayList<SelectItem>();
    
    
    for (LineItem lineItem : lineItems) {
    
    selectItems.add(new SelectItem(lineItem.getProduct().getId(), lineItem.getProduct().getProductName()));
    }
    return selectItems;
    }
    }
    

    public Company getCompanyByName(String companyName) {

        Query query = em.createQuery("SELECT c FROM Company c WHERE c.name = :inCompanyName");
        query.setParameter("inCompanyName", companyName);
        Company company = null;
        try {
            company = (Company) query.getSingleResult();
        } catch (NoResultException ex) {
            ex.printStackTrace();
        }
        return company;
    }

    public SalesQuotation getQuotationById(Long quotationId) throws QuotationNotFoundException {
//        Query query = em.createQuery("SELECT q FROM SalesQuotation q WHERE q.id = :inQuotationId");
//        query.setParameter("inQuotationId",quotationId );
//        SalesQuotation quotation = new SalesQuotation();
//        try{
//        quotation = (SalesQuotation)query.getSingleResult();
//        }
//        catch(NoResultException ex){
//            ex.printStackTrace();
//        }
        SalesQuotation quotation = em.find(SalesQuotation.class, quotationId);
        if (quotation == null)
        {
            throw new QuotationNotFoundException();
        }
        return quotation;
    }
}
